//
//  HomeViewController.swift
//  Swift微博
//
//  Created by 陈竹 on 16/2/21.
//  Copyright © 2016年 陈竹. All rights reserved.
//

import UIKit
//cell可重用id
private let CZStatusCellID="CZStatusCellID"
class HomeViewController: RootViewController {

    override func viewDidLoad() {
       //设置刷新控件
        setupRefreshControl()
        super.viewDidLoad()
//        //注册可重用cell
        tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: CZStatusCellID)
        visitorView?.setupInfo(message: "关注一些人，回这里看看有什么惊喜")
        
    }

  
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func loadData() {
        let defaultIndex=100
        //如果上啦刷新动画开始
        let isPullStart=pullUpView.isAnimating()
        if !isPullStart{
            //只会显示菊花，不会调用方法
            refreshControl?.beginRefreshing()
        }
        
        
        dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in
            let pullDownIndex=self.dataList.first ?? defaultIndex
           
            let pullUpIndex=self.dataList.last ?? 0
            
            var arrayM=[Int]()
          NSThread.sleepForTimeInterval(2)
            if isPullStart{
                for i in 0..<10{
                arrayM.append(pullUpIndex-1-i)
                }
            self.dataList+=arrayM
            }else{
            
            for i in (0..<20).reverse(){
                arrayM.append(i+pullDownIndex+1)
                }
                self.dataList=arrayM + self.dataList
            }
            
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
             
                //停止上啦刷新动画
                self.pullUpView.stopAnimating()
                //关闭刷新
                self.refreshControl?.endRefreshing()
                //刷新数据
                self.tableView?.reloadData()
            })
            
        }
    }
    private lazy var dataList=[Int]()
}

extension HomeViewController{

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell=tableView.dequeueReusableCellWithIdentifier(CZStatusCellID, forIndexPath: indexPath)
        cell.textLabel?.text=dataList[indexPath.row].description
    return cell
    }
    
}
